﻿using System;
using Epm.AppLogic.Framework;
using msBsc.Xsd.DataTds;
using System.Data;
using System.Text;

namespace msBsc.AppLogic.DataAlc
{
    /// <summary>
    /// 美食信息类
    /// </summary>
    public class CBizPushNiceFood : CustBizObject
    {
        private CDmDjkPushNiceFood m_CDmDjkPushNiceFood = new CDmDjkPushNiceFood();
        private CDmDjkPushNiceFood oDmDjkPushNiceFood
        {
            get { return m_CDmDjkPushNiceFood; }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public CBizPushNiceFood()
            : base()
        {
            this.DataObj = new CDbAccess();
            this.DataObj.Init(this.m_CDmDjkPushNiceFood.DJK_PUSH_NICEFOOD);
            this.TableName = this.DataObj.TableName;
            this.DataObj.KeyField = this.DataObj.Fields["NICEFOOD_ID"];
        }

        /// <summary>
        /// 新增记录前的处理
        /// </summary>
        /// <param name="drw">新增数据行</param>
        /// <returns>true/false</returns>
        /// <remarks>
        /// 生成ID值
        /// </remarks>
        protected override bool BeforeRowAdded(DataRow drw)
        {
            if (drw[this.KeyFieldName].ToString().Length < 10)
            {
                drw[this.KeyFieldName] = this.DataObj.NewID();
            }
            return true;
        }

        /// <summary>
        /// 记录修改前检验
        /// </summary>
        /// <param name="drw">当前记录</param>
        /// <returns>>校验合法，返回值为true,否则返回false</returns>
        /// <remarks>
        /// ID不允许修改,并把此修改操作记录到BSE_MODIFY_HISTORY表中
        /// </remarks>
        protected override bool BeforeRowModified(DataRow drw)
        {
            //ID不容许修改
            if (drw[this.KeyFieldName].ToString() != drw[this.KeyFieldName, DataRowVersion.Original].ToString())
            {
                Exception e = new Exception(this.TableName + "表的主键" + this.KeyFieldName + "字段不允许被修改");
                throw e;
            }
            return true;
        }
        protected override bool BeforeRowDeletedR(DataRow drw)
        {
            //using (msBsc.AppLogic.DataAlc.CBizShpPageColumn objtem = new CBizShpPageColumn())
            //{
            //    DataSet ds = objtem.SelectByPageNo(this.GetRowStr(drw, KeyFieldName));
            //    for (int i = ds.Tables[0].Rows.Count - 1; i >= 0; i--)
            //    {
            //        ds.Tables[0].Rows[i].Delete();
            //    }
            //    objtem.Update(ds);
            //} 
            return true;
        }

        protected override bool AfterRowAdded(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowModified(DataRow drw)
        {
            return true;
        }

        protected override bool AfterRowDeletedR(DataRow drw)
        {
            return true;
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="pageRows">每页多少条记录</param>
        /// <param name="pageNum">第几页</param>
        /// <param name="destionId">目的地编号</param>
        /// <param name="cityId">城市编号</param>
        /// <param name="countryId">国家编号</param>
        /// <returns></returns>
        public DataSet GetNiceFoodForPageview(int pageRows, int pageNum, string destionId, string cityId, string countryId)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("ipagerows", ParameterDirection.Input, EDbType.String, 36, pageRows));
            parmCollection.Add(new CDbParm("ipagenum", ParameterDirection.Input, EDbType.String, 36, pageNum));
            parmCollection.Add(new CDbParm("destination", ParameterDirection.Input, EDbType.String, 36, destionId));
            parmCollection.Add(new CDbParm("cityid", ParameterDirection.Input, EDbType.String, 36, cityId));
            parmCollection.Add(new CDbParm("countryid", ParameterDirection.Input, EDbType.String, 36, countryId));
            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_PUSH_NICEFOOD_FOR_VIEW", parmCollection);

            return ds;
        }

        public DataSet GetNiceFoodForPageview(int pageRows, int pageNum, string destionId, string cityId, string countryId, string foodKey)
        {
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("ipagerows", ParameterDirection.Input, EDbType.String, 36, pageRows));
            parmCollection.Add(new CDbParm("ipagenum", ParameterDirection.Input, EDbType.String, 36, pageNum));
            parmCollection.Add(new CDbParm("destination", ParameterDirection.Input, EDbType.String, 36, destionId));
            parmCollection.Add(new CDbParm("cityid", ParameterDirection.Input, EDbType.String, 36, cityId));
            parmCollection.Add(new CDbParm("countryid", ParameterDirection.Input, EDbType.String, 36, countryId));
            parmCollection.Add(new CDbParm("foodKey", ParameterDirection.Input, EDbType.String, 255, foodKey));
            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_PUSH_NICEFOOD_FOR_VIEW", parmCollection);

            return ds;
        }

        /// <summary>
        /// 获取美食信息
        /// </summary>
        /// <param name="destinId">目的地编号</param>
        /// <param name="cityId">城市编号</param>
        /// <param name="countryId">国家编号</param>
        /// <returns>NICEFOOD_ID：美食ID，NICEFOOD_NAME：美食名称，NICEFOOD_DEFAULT_PIC：美食图片</returns>
        public DataSet GetFoodsDataSet(string destinId, string cityId, string countryId)
        {
            //判断地区是否全为空
            if ((string.IsNullOrEmpty(destinId) &&
                string.IsNullOrEmpty(cityId) &&
                string.IsNullOrEmpty(countryId)))
            {
                return null;
            }

            //SQL语句
            StringBuilder sbSqlString = new StringBuilder();

            sbSqlString.Append("SELECT NICEFOOD_ID AS ID,NICEFOOD_NAME AS NAME,NICEFOOD_DEFAULT_PIC AS PIC FROM  DJK_PUSH_NICEFOOD  WHERE NICEFOOD_IS_DELETED = 0 AND NICEFOOD_IS_DISPLAY = 1 ");

            //SQL参数
            CDbParmCollection parmCollection = new CDbParmCollection();

            #region 判断地区信息
            if (!string.IsNullOrEmpty(destinId))
            {
                sbSqlString.Append("AND NICEFOOD_DISTINATION = @DESTINID ");
                parmCollection.Add(new CDbParm("DESTINID", ParameterDirection.Input, EDbType.String, 36, destinId.Replace("'", "").Replace(",", "','")));
            }

            if (!string.IsNullOrEmpty(cityId))
            {
                sbSqlString.Append("AND NICEFOOD_CITY = @CITYID ");
                parmCollection.Add(new CDbParm("CITYID", ParameterDirection.Input, EDbType.String, 36, cityId.Replace("'", "").Replace(",", "','")));
            }

            if (!string.IsNullOrEmpty(countryId))
            {
                sbSqlString.Append("AND NICEFOOD_COUNTRY = @COUNTRYID ");
                parmCollection.Add(new CDbParm("COUNTRYID", ParameterDirection.Input, EDbType.String, 36, countryId.Replace("'", "").Replace(",", "','")));
            }
            #endregion

            sbSqlString.Append("ORDER BY NICEFOOD_IS_SPECIE DESC,NICEFOOD_UPLOAD_TIME DESC");
            //执行查询
            DataSet ds = this.DataObj.QueryBySQL(sbSqlString.ToString(), parmCollection);

            return ds;
        }

        /// <summary>
        /// 根据美食编号查询美食信息
        /// </summary>
        /// <param name="nicefoodId">美食ID</param>
        /// <returns>NICEFOOD_ID:美食ID，NICEFOOD_NAME：美食名称，NICEFOOD_DEFAULT_PIC：美食图片，NICEFOOD_DETAILS：美食详情，NICEFOOD_DISTINATION：目的地编号，NICEFOOD_CITY：城市编号，NICEFOOD_COUNTRY：国家编号，COUNTRYNAME：国家名称，COUNTRY_CITY_NME：城市名称，DESTINATIONNAME：目的地名称</returns>
        public DataSet GetScenicById(string nicefoodId)
        {
            if (string.IsNullOrEmpty(nicefoodId))
            {
                return null;
            }

            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("NICEFOODID", ParameterDirection.Input, EDbType.String, 36, nicefoodId.Replace("'", "").Replace(",", "','")));

            DataSet ds = this.DataObj.QueryBySQL(@"SELECT T3.*,A3.COUNTRY_CITY_NME DESTINATIONNAME FROM 
    (SELECT T2.*,A2.COUNTRY_CITY_NME CITYNAME FROM 
    (SELECT T1.*,A1.COUNTRY_CITY_NME COUNTRYNAME FROM 
    (SELECT S.NICEFOOD_ID AS ID ,S.NICEFOOD_NAME AS NAME ,S.NICEFOOD_DEFAULT_PIC PICTURE,S.NICEFOOD_DETAILS AS DETAILS,
    S.NICEFOOD_DISTINATION AS DISTIANTIONID ,S.NICEFOOD_CITY AS CITYID,
    S.NICEFOOD_COUNTRY AS COUNTRYID 
    FROM DJK_PUSH_NICEFOOD S
    WHERE S.NICEFOOD_ID=@NICEFOODID ) T1 
    LEFT JOIN DJK_COUNTRY_CITY A1 
    ON T1.COUNTRYID=A1.COUNTRY_CITY_ID) T2 
    LEFT JOIN DJK_COUNTRY_CITY A2 
    ON T2.CITYID=A2.COUNTRY_CITY_ID) T3 
    LEFT JOIN DJK_COUNTRY_CITY A3 
    ON T3.DISTIANTIONID=A3.COUNTRY_CITY_ID", parmCollection);

            return ds;
        }

        /// <summary>
        /// 根据编号查询美食信息包含上一条下一条
        /// </summary>
        /// <param name="scenicId">美食编号</param>
        /// <returns>查询结果</returns>
        public DataSet GetNiceFoodByIdWithPrevNext(string nicefoodId)
        {
            if (string.IsNullOrEmpty(nicefoodId))
            {
                return null;
            }
            CDbParmCollection parmCollection = new CDbParmCollection();
            parmCollection.Add(new CDbParm("nicefoodId", ParameterDirection.Input, EDbType.String, 36, nicefoodId));

            DataSet ds = this.DataObj.QueryBySP("DJK_SELECT_PREVIEW_NEXT_NICEFOOD_FOR_VIEW", parmCollection);

            return ds;
        }


        /// <summary>
        /// 发布美食信息
        /// </summary>
        /// <param name="niceFoodName"></param>
        /// <param name="niceFoodAddr"></param>
        /// <param name="niceFoodPrice"></param>
        /// <param name="niceFoodIsSpecie"></param>
        /// <param name="defaultPic"></param>
        /// <param name="desId"></param>
        /// <param name="cityId"></param>
        /// <param name="countryId"></param>
        /// <param name="fileUrl"></param>
        /// <returns></returns>
        public string[] AddNiceFoodInfo(string niceFoodName, string niceFoodAddr, string niceFoodPrice, bool niceFoodIsSpecie, string defaultPic, string desId, string cityId, string countryId, string niceFoodContent)
        {
            if (string.IsNullOrEmpty(niceFoodName)
                || string.IsNullOrEmpty(defaultPic))
            {
                return new string[] { "false", "美食名称或封面图片不能为空" };
            }
            if (niceFoodName.Length > 100)
            {
                return new string[] { "false", "美食名称需在100个字符以内" };
            }
            if (string.IsNullOrEmpty(desId)
                || string.IsNullOrEmpty(cityId)
                || string.IsNullOrEmpty(countryId))
            {
                return new string[] { "false", "地区信息有误，请检查" };
            }

            string strVpath = msBsc.AppLogic.Cfg.SystemFileDir.FileDir.strSaveUploadSysFileDirV + this.DataObj.NewID() + ".html";//存储文件的虚拟路径
            if (niceFoodContent != string.Empty && strVpath != string.Empty && niceFoodContent != "<br />")
            {
                string strFFullPath = System.Web.HttpContext.Current.Server.MapPath(strVpath);
                using (System.IO.TextWriter streamWriter = new System.IO.StreamWriter(strFFullPath))
                {
                    streamWriter.Write(niceFoodContent);
                }
            }
            else
            {
                return new string[] { "false", "文件信息有误，请重试！" };
            }

            DataSet ds = new CDmDjkPushNiceFood();
            DataRow dr = ds.Tables[0].NewRow();

            dr["NICEFOOD_ID"] = this.DataObj.NewID();
            dr["NICEFOOD_NAME"] = niceFoodName;
            dr["NICEFOOD_ADDRESS"] = niceFoodAddr;
            dr["NICEFOOD_PRICE"] = niceFoodPrice;
            dr["NICEFOOD_IS_SPECIE"] = niceFoodIsSpecie;
            dr["NICEFOOD_DEFAULT_PIC"] = defaultPic;
            dr["NICEFOOD_UPLOAD_TIME"] = this.dtCurrDateTime;
            dr["NICEFOOD_DISTINATION"] = desId;
            dr["NICEFOOD_CITY"] = cityId;
            dr["NICEFOOD_COUNTRY"] = countryId;
            dr["CONTENT_FILE_URL"] = strVpath;
            dr["NICEFOOD_IS_DELETED"] = false;
            dr["NICEFOOD_IS_DISPLAY"] = true;
            dr["NICEFOOD_DETAILS"] = "TEST";

            ds.Tables[0].Rows.Add(dr);
            this.Update(ds);

            return new string[] { "true", "发布成功" };
        }


        public DataSet GetAllNiceFoodInfo()
        {
            DataSet ds = this.DataObj.QueryBySQL(@"SELECT * FROM DJK_PUSH_NICEFOOD");
            ds.Tables[0].TableName = this.TableName;
            return ds;
        }
    }
}
